#include "sema_thread.h"

void* producer(void *arg)
{
 char item;
 while(1)
 {
  printf("producer:\n");
    item = produce_item();
    printf("produced item was %c\n",item);
    sem_wait(&empty);
    sem_wait(&mutex);
    insert_item(item);
    sem_post(&mutex);
    sem_post(&full);
 }

}

void* consumer(void *arg)
{
 char item;
 while(1)
 {
  sem_wait(&full);
  sem_wait(&mutex);
  item = remove_item();
  printf("consumer:\n");
  sem_post(&mutex);
  sem_post(&empty);                                                  
  consume_item(item);
 }
                                                                                         
}

char produce_item()
{
 char read_buffer[SIZE];

 if((read(fd,read_buffer,SIZE))!=SIZE)
    {
     perror("Failed to read:");
     exit(0);
    }
 return read_buffer[0];

}

void insert_item(char item)
{
 items[in] = item;
 in++;
}

char remove_item()
{
 in--;
 return items[in];
}

void consume_item(char item)
{
 printf("Consumed item was %c\n",item);
}


